/**
 * @author AIUHD74
 */

var events = [{
	"Name" : "Mad Cow ourbreak!",
	"Description" : "There has been an outbreak of Mad Cow disease.Beef is now more expensive.",
	"Effect" : "food,beef,price,increase,.50" //effect format:"type,"

}, {
	"Name" : "Bird Flu scare",
	"Description" : "Fly like an eagle away from the poultry products",
	"Effect" : "food,poultry,price,increase,.50" //effect format:"type,"
}, {
	"Name" : "Swine Flu scare",
	"Description" : "Swine flu oink oink",
	"Effect" : "food,pork,price,increase,.50" //effect format:"type,"
}];

var eventQueue = new Array();
var activeEventsQueue = new Array();

setInterval(function() {
		randEventTimer();
	}, 1000);

populateeventQueue();

function randEventTimer() {

	var rand = randomIntFromInterval(1, 50);
	if (rand == 32) {
		randEvent();

	}
}

function removeEvent(name) {
	notify("Event " + name + " is ending");
	writeToConsole("Event " + name + " is ending");
	var index = activeEventsQueue.indexOf(name);
	activeEventsQueue.splice(index, 1);
	refresheventTextArea();
}



function randEvent() {
	var randevent = eventQueue[Math.floor((Math.random() * eventQueue.length) + 0)];
	var interval = randomIntFromInterval(300000, 600000);
	if (activeEventsQueue.length == 0)// if there are no active events then we need to push one
	{
		activeEventsQueue.push(randevent);
		objx(events).each(function(item) {
			if (item.Name == randevent) {
				notify("Event:" + item.Name + " has started.");
				writeToConsole("Event:" + item.Name + " has started.");
				setTimeout(function() {
					removeEvent(randevent);
				}, interval);
				refresheventTextArea();

			}
		});
	} else {

		if (activeEventsQueue.indexOf(randevent) == -1)// if the random event is not already in effect then we should push it
		{
			activeEventsQueue.push(randevent);
			objx(events).each(function(item) {
				if (item.Name == randevent) {
					notify("Event:" + item.Name + " has started.");
					writeToConsole("Event:" + item.Name + " has started.");
					setTimeout(function() {
						removeEvent(randevent);
					}, interval);
					refresheventTextArea();

				}
			});

		} else {
			//writeToConsole("Event:" + randevent + " is already happening");

		}
	}
}

function populateeventQueue() {
	objx(events).each(function(item) {
		eventQueue.push(item.Name);
	});

}




